package com.hnucm.house.ui.fragment;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.ColorUtils;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.google.gson.Gson;
import com.hnucm.house.R;
import com.hnucm.house.adapter.HomeTopAdapter;
import com.hnucm.house.base.BaseFragment;
import com.hnucm.house.base.BasePresenter;
import com.hnucm.house.bean.HomeTop.Constant;
import com.hnucm.house.bean.HomeTop.HomeTop;
import com.hnucm.house.bean.HomeTop.TopResponse;
import com.hnucm.house.custom.HIndicator;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HomeFragment extends BaseFragment {

    @BindView(R.id.home_tab)
    QMUITabSegment mSlidingTabLayout;

    @BindView(R.id.home_viewpager)
    ViewPager mViewPager;

    @BindView(R.id.home_recycler)
    RecyclerView mHomeTop;

    @BindView(R.id.hIndicator)
    HIndicator mHIndicator;

    private int mCurTab;

    private Context mContext;

    private List<String> mTabNames = new ArrayList<>();

    private ArrayList<Fragment> mFragmentArrayList = new ArrayList<>();

    private List<TopResponse.DataDTO> mHomeTopList = new ArrayList<>();
    private HomeTopAdapter homeTopAdapter;

    public static HomeFragment getInstance(){
        HomeFragment homeFragment = new HomeFragment();
        return homeFragment;
    }
    @Override
    protected int getContentViewId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void init() {
        mContext = getActivity().getApplicationContext();
        initStatusBar();
        mCurTab = 0;
        initFragment();
        intiViewPager();
        initData();
        initAdapter();
    }

    private void initData() {
        TopResponse topResponse = new Gson().fromJson(Constant.TOP_JSON, TopResponse.class);
        mHomeTopList.addAll(topResponse.getData());
//        TopResponse.DataDTO homeTop = new TopResponse.DataDTO();
//        homeTop.setDesc("购房百科");
//        homeTop.setPic(String.valueOf(R.drawable.ic_baike));
//        mHomeTopList.add(homeTop);

    }


    private void initAdapter() {
        homeTopAdapter = new HomeTopAdapter(mHomeTopList, mContext);
        GridLayoutManager manager = new GridLayoutManager(mContext, 1, LinearLayoutManager.HORIZONTAL, false);
        mHomeTop.setLayoutManager(manager);
        mHomeTop.setAdapter(homeTopAdapter);
        mHIndicator.bindRecyclerView(mHomeTop);
    }

    private void initStatusBar() {
        getActivity().getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
        if (ColorUtils.calculateLuminance(com.blankj.utilcode.util.ColorUtils.getColor(R.color.white)) >= 0.5) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    private void initFragment() {
        mTabNames.add(0,"关注");
        mTabNames.add(1,"推荐");
        mTabNames.add(2,"分类");
        mFragmentArrayList.add(0,new NewHouseFragment());
        mFragmentArrayList.add(1,new NewHouseFragment());
        mFragmentArrayList.add(2,new NewHouseFragment());
    }
    private void intiViewPager() {
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentArrayList == null ? 0: mFragmentArrayList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return mTabNames.get(position);
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurTab = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mSlidingTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
